package com.mlearning.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mlearning.demo.mapper.BookMapper;
import com.mlearning.demo.mapper.GraphMapper;
import com.mlearning.demo.pojo.neo4j.Book;
import com.mlearning.demo.pojo.utils.Group;
import com.mlearning.demo.pojo.utils.Link;
import com.mlearning.demo.pojo.utils.Node;
import com.mlearning.demo.pojo.utils.Triad;
import com.mlearning.demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("bookService")
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;

//    private List<Integer> nodeList;
//    private List<Integer> linkList;
    @Override
    public Book getBookDetail(int id){
        return bookMapper.getBookDetail(id);
    }
    @Override
    public ArrayList<String> getBookTags(int id){
//        ArrayList<String> tags = bookMapper.getBookTags(id);
//        if (tags.size()==0){
//            return null;
//        }
//        nodeList=new ArrayList<>();
//        linkList=new ArrayList<>();
//        Group nodes=new Group();
//        Group links=new Group();


        return bookMapper.getBookTags(id);
    }

    @Override
    public ArrayList<String> getBookKnows(int id){
        return  bookMapper.getBookKnows(id);
    }
    @Override
    public boolean isSave(int userid,int bookid){
//        System.out.println(bookMapper.isSave(userid, bookid));
        return bookMapper.isSave(userid, bookid)>0;
    }
    @Override
    public  ArrayList<JSONObject> getRecBooks(int userid){
        ArrayList<Book> recBooks=bookMapper.getRecBooks(userid);
        ArrayList<JSONObject> recBooksJson=new ArrayList<>();
        if(recBooks.size()<6){
            recBooks.addAll(bookMapper.getTopBooks());
        }
        for(Book book:recBooks){
            int bookid = book.getId();
            ArrayList<String> tag = bookMapper.getBookTags(bookid);
            JSONObject bookJson=new JSONObject();
            bookJson.put("book",book);
            bookJson.put("tag",tag);
            recBooksJson.add(bookJson);
        }
        return recBooksJson;
    }
    @Override
    public ArrayList<Book> getRelBooks(String search){
        return bookMapper.getRelBooks(search);
    }

//    private void addJson(List<Triad> triads,Group nodes,Group links){
//        JSONObject start;
//        JSONObject rel;
//        JSONObject end;
//        int id;
//        for (Triad triad:triads
//        ) {
//            start=JSON.parseObject(triad.getStartnode());
//            id =start.getInteger("id");
//
//            if(!nodeList.contains(id))
//            {
//                nodeList.add(id);
//                Node node=new Node(start.getString("name"),0);
//                nodes.getList().add(node);
//            }
//            int source=nodeList.indexOf(id);
//
//            end= JSON.parseObject(triad.getEndnode());
//            id= end.getInteger("id");
//            if(!nodeList.contains(id))
//            {
//                nodeList.add(id);
//                Node node=new Node(end.getString("name"),1);
//                nodes.getList().add(node);
//            }
//            int target=nodeList.indexOf(id);
//
//            rel=JSON.parseObject(triad.getRelationship());
//            id=rel.getInteger("id");
//            if(!linkList.contains(id)){
//                linkList.add(id);
//                Link link=new Link(source,target);
//                links.getList().add(link);
//            }
//        }

//    }
}
